﻿Imports System.Globalization

Public Class frmNotasCredito

    Private Sub frmNotasCredito_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvdetalle.AutoGenerateColumns = False
        btnImprimir.Visible = False

        cboEstado.SelectedIndex = 0
        cboMotivo.SelectedIndex = 0

        oiml.CargarCombos(cboIVA, "Impuesto", "idImpuesto", controllerStock.get_impuestos(oiml.dbConn, 0, ""))

        CargarDatos()

    End Sub

    Private _codDevolucion As Integer = 0

    Public Property codDevolucion() As Integer
        Get
            Return _codDevolucion
        End Get
        Set(ByVal value As Integer)
            _codDevolucion = value
        End Set
    End Property

    Dim controller As New ControllerSigeco.controller_compras
    Dim controllerVtas As New ControllerSigeco.controller_ventas
    Dim controllerStock As New ControllerSigeco.controller_stock
    Dim controllerCompras As New ControllerSigeco.controller_compras

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private _idNotaCredito As Integer = 0

    Public Property codNotaCredito() As Integer
        Get
            Return _idNotaCredito
        End Get
        Set(ByVal value As Integer)
            _idNotaCredito = value
        End Set
    End Property

    Private _codProveedor As Integer = 0

    Public Property codProveedor() As Integer
        Get
            Return _codProveedor
        End Get
        Set(ByVal value As Integer)
            _codProveedor = value
        End Set
    End Property

    Private _codCliente As Integer = 0

    Public Property codCliente() As Integer
        Get
            Return _codCliente
        End Get
        Set(ByVal value As Integer)
            _codCliente = value
        End Set
    End Property

    Private _codNotaCreditoDetalle As Integer = 0

    Public Property codNotaCreditoDetalle() As Integer
        Get
            Return _codNotaCreditoDetalle
        End Get
        Set(ByVal value As Integer)
            _codNotaCreditoDetalle = value
        End Set
    End Property

    Private _codCompra As Integer = 0

    Public Property codCompra() As Integer
        Get
            Return _codCompra
        End Get
        Set(ByVal value As Integer)
            _codCompra = value
        End Set
    End Property

    Private _codVenta As Integer = 0

    Public Property codVenta() As Integer
        Get
            Return _codVenta
        End Get
        Set(ByVal value As Integer)
            _codVenta = value
        End Set
    End Property

    Private m_frmBusMercaderia As frmMercaderiaBuscador
    Public Property frmBusMercaderia() As frmMercaderiaBuscador
        Get
            Return m_frmBusMercaderia
        End Get
        Set(ByVal value As frmMercaderiaBuscador)
            m_frmBusMercaderia = value
        End Set
    End Property

    Private m_frmBusProveedor As frmProveedorBuscador
    Public Property frmBusProveedor() As frmProveedorBuscador
        Get
            Return m_frmBusProveedor
        End Get
        Set(ByVal value As frmProveedorBuscador)
            m_frmBusProveedor = value
        End Set
    End Property

    Private m_frmBusCliente As frmClienteBuscador
    Public Property frmBusCliente() As frmClienteBuscador
        Get
            Return m_frmBusCliente
        End Get
        Set(ByVal value As frmClienteBuscador)
            m_frmBusCliente = value
        End Set
    End Property

    Private Sub btnBuscProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscProveedor.Click

        codVenta = 0
        codCompra = 0
        txtFactura.Text = ""

        frmBusProveedor = New frmProveedorBuscador
        frmBusProveedor.ShowDialog()
        txtProveedor.Text = frmBusProveedor.ProveedorDesc
        codProveedor = frmBusProveedor.codProveedor

        If codProveedor > 0 Then

        End If

    End Sub

    Private Sub btnBuscCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btBuscarCliente.Click

        codVenta = 0
        codCompra = 0
        txtFactura.Text = ""

        frmBusCliente = New frmClienteBuscador
        frmBusCliente.ShowDialog()
        txtCliente.Text = frmBusCliente.ClienteDesc
        codCliente = frmBusCliente.codCliente

        If codCliente > 0 Then

        End If

    End Sub

    Private Sub txtcantidad_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcantidad.Leave
        If IsNumeric(txtcantidad.Text) Then

        End If
    End Sub

    Private Sub CargarDetalles()
        Try

            txtMercaderia.Text = ""
            txtcantidad.Text = 1

            Dim ds As New DataTable
            ds = controllerVtas.get_NotasCredito_Detalles_All(oiml.dbConn, codNotaCredito)

            If Not ds Is Nothing Then
                dgvdetalle.DataSource = ds
                cboMotivo.Enabled = False
                btnSeleccionFactura.Enabled = False
            Else
                dgvdetalle.DataSource = Nothing
                cboMotivo.Enabled = True
                btnSeleccionFactura.Enabled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Dim ci As CultureInfo

    Private Sub CargarDatos()
        Try
            If codNotaCredito > 0 Then

                Dim ds As New DataTable
                ds = controllerVtas.get_NotasCredito_by_ID(oiml.dbConn, codNotaCredito)

                If Not ds Is Nothing Then

                    txtActualizadoFecha.Text = ds.Rows(0)("actualizadoFecha_")
                    txtActualizadoPor.Text = ds.Rows(0)("usuario")

                    nroRemision.Text = ds.Rows(0)("nroComprobante")
                    fechaFactura.Value = ds.Rows(0)("fecha")
                    cboEstado.Text = ds.Rows(0)("estado")

                    txtTimbrado.Text = ds.Rows(0)("nroTimbrado").ToString()
                    fechaTimbrado.Value = ds.Rows(0)("vtoTimbrado")
                    cboMotivo.Text = ds.Rows(0)("tipo")

                    CargarDetalles()
                    ControlEstados()

                    If ds.Rows(0)("nroVenta").ToString() <> "" Then
                        txtFactura.Text = ds.Rows(0)("nroVenta")
                        codVenta = ds.Rows(0)("idVenta")
                    ElseIf ds.Rows(0)("nroCompra").ToString() <> "" Then
                        txtFactura.Text = ds.Rows(0)("nroCompra")
                        codCompra = ds.Rows(0)("idCompra")
                    End If

                    If ds.Rows(0)("idNotaRemision").ToString() <> "" Then
                        codDevolucion = ds.Rows(0)("idNotaRemision").ToString()
                        txtDevolucion.Text = ds.Rows(0)("nroDevolucion").ToString()
                    End If

                    If ds.Rows(0)("idCliente").ToString() <> "" Then

                        txtCliente.Text = ds.Rows(0)("cliente")
                        codCliente = ds.Rows(0)("idCliente")

                    Else

                        txtProveedor.Text = ds.Rows(0)("proveedor")
                        codProveedor = ds.Rows(0)("idProveedor")

                    End If

                    

                    

                End If

            Else
                cboEstado.Text = "EN PROCESO"
                nroRemision.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub ControlEstados()

        If cboEstado.Text = "APROBADA" Then

            grpDetalles.Visible = False
            nroRemision.Enabled = False
            fechaFactura.Enabled = False
            fechaTimbrado.Enabled = False
            cboMotivo.Enabled = False
            txtTimbrado.Enabled = False
            btnBuscProveedor.Enabled = False
            btBuscarCliente.Enabled = False

            btnGrabar.Enabled = False
            btnAgregarDetalle.Enabled = False

            btnEliminar.Enabled = False
            btnEliminarDetalle.Enabled = False

            btnConfirmar.Visible = False

            txtcantidad.Enabled = False
            btnSeleccionFactura.Enabled = False
            btnDevolucion.Enabled = False

            btnCancelar.Focus()


        ElseIf cboEstado.Text = "ANULADA" Then

            grpDetalles.Visible = False
            nroRemision.Enabled = False
            fechaFactura.Enabled = False
            fechaTimbrado.Enabled = False
            cboMotivo.Enabled = False

            txtTimbrado.Enabled = False
            btnBuscProveedor.Enabled = False
            btBuscarCliente.Enabled = False

            btnGrabar.Enabled = False
            btnAgregarDetalle.Enabled = False
            btnEliminarDetalle.Enabled = False

            btnConfirmar.Enabled = False

            btnEliminar.Enabled = False
            btnAnular.Enabled = False

            txtcantidad.Enabled = False
            btnSeleccionFactura.Enabled = False
            btnDevolucion.Enabled = False

            btnCancelar.Focus()

        End If

    End Sub

    Private Function ValidarCampos(ByVal verificarProvee As Boolean) As Boolean

        Dim controlCampos As Boolean = False

        If nroRemision.Text.Replace("_", "").Replace("-", "").Trim() = "" Then
            ErrorProvider1.SetError(nroRemision, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(nroRemision, "")
        End If        '

        If cboMotivo.Text.Contains("VENTA") Then
            If txtCliente.Text = "" Or codCliente = 0 Then
                ErrorProvider1.SetError(txtCliente, "Campo obligatorio")
                controlCampos = True
            Else
                ErrorProvider1.SetError(txtCliente, "")
                ErrorProvider1.SetError(txtProveedor, "")
            End If        '

        Else

            If verificarProvee Then
                If txtProveedor.Text = "" Or codProveedor = 0 Then
                    ErrorProvider1.SetError(txtProveedor, "Campo obligatorio")
                    controlCampos = True
                Else
                    ErrorProvider1.SetError(txtProveedor, "")
                    ErrorProvider1.SetError(txtCliente, "")
                End If        '
            End If
            
        End If

        If txtTimbrado.Text = "" Then
            ErrorProvider1.SetError(txtTimbrado, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtTimbrado, "")
        End If        '

        Return controlCampos

    End Function

    Private Function ValidarCamposDetalle() As Boolean

        Dim controlCampos As Boolean = False

        If txtMercaderia.Text = "" Then
            ErrorProvider1.SetError(txtMercaderia, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtMercaderia, "")
        End If        '

        If txtcantidad.Text = "" Or Not IsNumeric(txtcantidad.Text) Then
            ErrorProvider1.SetError(txtcantidad, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtcantidad, "")
        End If        '

        If txttotal.Text = "" Or Not IsNumeric(txttotal.Text) Then
            ErrorProvider1.SetError(txttotal, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txttotal, "")
        End If        '

        Return controlCampos

    End Function

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        If codNotaCredito > 0 Then

            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Nota de credito", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                If controllerVtas.Delete_NotasCredito(oiml.dbConn, codNotaCredito, oiml.idUsuario) Then
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & controllerVtas.MessageError.Message, "Nota de credito", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabar.Click

        If ValidarCampos(True) Then
            Exit Sub
        End If

        If codNotaCredito = 0 Then
            codNotaCredito = controllerVtas.Insert_NotasCredito(oiml.dbConn, nroRemision.Text, txtTimbrado.Text, fechaTimbrado.Value, fechaFactura.Value, codCliente, codProveedor, cboEstado.Text, oiml.idUsuario, codVenta, codCompra, codDevolucion)

            If codNotaCredito > 0 Then

                CargarDatos()
                MessageBox.Show("Registro grabado con exito!", "Nota de credito", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            Else
                MessageBox.Show("Error: " & controllerVtas.MessageError.Message, "Nota de credito", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If controllerVtas.Update_NotasCredito(oiml.dbConn, codNotaCredito, nroRemision.Text, txtTimbrado.Text, fechaTimbrado.Value, fechaFactura.Value, codCliente, codProveedor, cboEstado.Text, oiml.idUsuario, codVenta, codCompra, codDevolucion) Then

                CargarDatos()
                MessageBox.Show("Registro grabado con exito!", "Nota de credito", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)


            Else
                MessageBox.Show("Error: " & controllerVtas.MessageError.Message, "Nota de credito", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    Private Sub dgvdetalle_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvdetalle.KeyDown
        If e.KeyCode = Keys.Enter Then
            If dgvdetalle.SelectedRows.Count > 0 Then

                codNotaCreditoDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

            End If
        End If
    End Sub

    Private Sub dgvdetalle_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvdetalle.DoubleClick
        If dgvdetalle.SelectedRows.Count > 0 Then

            codNotaCreditoDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

        End If
    End Sub

    Private Sub dgvdetalle_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvdetalle.CellClick
        Try

            If dgvdetalle.SelectedRows.Count > 0 Then

                codNotaCreditoDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnEliminarDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminarDetalle.Click

        If codNotaCreditoDetalle > 0 Then

            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Nota de credito", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                If controllerVtas.Delete_NotasCredito_Detalle(oiml.dbConn, codNotaCreditoDetalle, oiml.idUsuario, codNotaCredito) Then
                    CargarDatos()

                Else
                    MessageBox.Show("Error: " & controllerVtas.MessageError.Message, "Remisiones", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Sub btnAgregarDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarDetalle.Click

        If ValidarCampos(True) Then
            Exit Sub
        End If

        If ValidarCamposDetalle() Then
            Exit Sub
        End If

        If codNotaCredito = 0 Then

            codNotaCredito = controllerVtas.Insert_NotasCredito(oiml.dbConn, nroRemision.Text, txtTimbrado.Text, fechaTimbrado.Value, fechaFactura.Value, codCliente, codProveedor, cboEstado.Text, oiml.idUsuario, codVenta, codCompra, codDevolucion)

            If codNotaCredito > 0 Then

            Else
                MessageBox.Show("Error: " & controllerVtas.MessageError.Message, "Nota de credito", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else

            If controllerVtas.Update_NotasCredito(oiml.dbConn, codNotaCredito, nroRemision.Text, txtTimbrado.Text, fechaTimbrado.Value, fechaFactura.Value, codCliente, codProveedor, cboEstado.Text, oiml.idUsuario, codVenta, codCompra, codDevolucion) Then

            Else
                MessageBox.Show("Error: " & controllerVtas.MessageError.Message, "Remisiones", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

        If codNotaCredito > 0 Then

            If controllerVtas.Insert_NotasCredito_Detalle(oiml.dbConn, codNotaCredito, Decimal.Parse(txtcantidad.Text), txtMercaderia.Text, Integer.Parse(cboIVA.SelectedValue), Decimal.Parse(txttotal.Text), codVenta, codCompra) Then
                CargarDatos()
                MessageBox.Show("Registro grabado con exito!", "Notas de credito", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            Else
                MessageBox.Show("Error: " & controllerVtas.MessageError.Message, "Notas de credito", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    'Private Sub btnAnular_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnular.Click
    '    If codCompra > 0 Then
    '        If MessageBox.Show("Desea anular el registro seleccionado?", "Compras", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

    '            If controller.Update_Compras_Estado(oiml.dbConn, codCompra, "ANULADA", oiml.idUsuario) Then
    '                Me.Close()

    '            Else
    '                MessageBox.Show("Error: " & controller.MessageError.Message, "Compras", MessageBoxButtons.OK, MessageBoxIcon.Error)

    '            End If

    '        End If
    '    End If
    'End Sub

    Private Sub btnBuscOrdenCompra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub btnConfirmar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirmar.Click
        If codNotaCredito > 0 Then

            If controllerVtas.Update_NotasCredito_Estado(oiml.dbConn, codNotaCredito, "APROBADA", oiml.idUsuario, nroRemision.Text) Then
                CargarDatos()
                MessageBox.Show("Registro confirmado con exito!", "Nota de credito", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else
                MessageBox.Show("Error: " & controllerVtas.MessageError.Message, "Nota de credito", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If

    End Sub

    Private Sub cboMotivo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboMotivo.SelectedIndexChanged

        If cboMotivo.Text.Contains("VENTA") Then
            btnBuscProveedor.Enabled = False
            btBuscarCliente.Enabled = True
            codProveedor = 0
            txtProveedor.Text = ""

            lblDevolucion.Visible = False
            txtDevolucion.Visible = False
            txtDevolucion.Text = ""
            btnDevolucion.Visible = False
        Else
            btnBuscProveedor.Enabled = True
            btBuscarCliente.Enabled = False
            codCliente = 0
            txtCliente.Text = ""

            lblDevolucion.Visible = True
            txtDevolucion.Visible = True
            txtDevolucion.Text = ""
            btnDevolucion.Visible = True
        End If

        codVenta = 0
        codCompra = 0
        codDevolucion = 0
        txtFactura.Text = ""

    End Sub

    Private m_frmBusFactura As frmFacturaBuscador
    Public Property frmBusFactura() As frmFacturaBuscador
        Get
            Return m_frmBusFactura
        End Get
        Set(ByVal value As frmFacturaBuscador)
            m_frmBusFactura = value
        End Set
    End Property

    Private m_frmBusDevolucion As frmDevolucionesBuscador
    Public Property frmBusDevolucion() As frmDevolucionesBuscador
        Get
            Return m_frmBusDevolucion
        End Get
        Set(ByVal value As frmDevolucionesBuscador)
            m_frmBusDevolucion = value
        End Set
    End Property

    Private Sub btnSeleccionFactura_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeleccionFactura.Click

        frmBusFactura = New frmFacturaBuscador
        frmBusFactura.conSaldo = False

        frmBusFactura.codVenta = 0
        frmBusFactura.codCompra = 0

        If codProveedor > 0 Then
            frmBusFactura.esCompra = True
            frmBusFactura.codCliente = codProveedor
        ElseIf codCliente > 0 Then
            frmBusFactura.esCompra = False
            frmBusFactura.codCliente = codCliente
        Else
            Return
        End If

        If frmBusFactura.ShowDialog() = Windows.Forms.DialogResult.OK Then

            codCompra = frmBusFactura.codCompra
            codVenta = frmBusFactura.codVenta

            Dim dt As New DataTable

            If codCompra > 0 Then

                dt = controller.get_Compras_ByID(oiml.dbConn, codCompra)
                If Not dt Is Nothing Then
                    codCompra = dt.Rows(0)("idCompra")
                    txtFactura.Text = dt.Rows(0)("nroComprobante")
                Else
                    MessageBox.Show("No se registran datos de la factura ingresada. Favor verifique la cuenta del proveedor.", "Informe", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If

            Else

                dt = controllerVtas.get_Ventas_ByID(oiml.dbConn, codVenta)
                If Not dt Is Nothing Then
                    codVenta = dt.Rows(0)("idVenta")
                    txtFactura.Text = dt.Rows(0)("nroComprobante")
                Else
                    MessageBox.Show("No se registran datos de la factura ingresada. Favor verifique la cuenta del proveedor.", "Informe", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If

            End If

        End If

    End Sub

    Private Sub btnDevolucion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDevolucion.Click
        frmBusDevolucion = New frmDevolucionesBuscador

        If ValidarCampos(False) Then
            Exit Sub
        End If

        If codProveedor > 0 Then
            frmBusDevolucion.codProveedor = codProveedor
        Else
            frmBusDevolucion.codProveedor = 0
        End If

        frmBusDevolucion.codCompra = codCompra
        frmBusDevolucion.conSaldo = True

        If frmBusDevolucion.ShowDialog() = Windows.Forms.DialogResult.OK Then

            codDevolucion = frmBusDevolucion.codDevolucion

            If MessageBox.Show("Esta seguro de asignar la devolución seleccionada?", "Seleccionar devolución", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.No Then
                Exit Sub
            End If

            Dim dt As New DataTable

            If codDevolucion > 0 Then

                dt = controller.get_Remision_ByID(oiml.dbConn, codDevolucion)

                If Not dt Is Nothing Then
                    codDevolucion = dt.Rows(0)("IdNotaRemision")
                    txtDevolucion.Text = dt.Rows(0)("NroComprobante")
                    codProveedor = dt.Rows(0)("idProveedor")
                    txtProveedor.Text = dt.Rows(0)("Proveedor")
                    fechaFactura.Text = dt.Rows(0)("fecha")

                    If Not dt.Rows(0)("idCompra") Is Nothing And dt.Rows(0)("idCompra").ToString() <> "" Then
                        codCompra = dt.Rows(0)("idCompra")
                    End If

                    txtFactura.Text = dt.Rows(0)("NroCompra")

                    btnGrabar_Click(sender, e)

                    If codNotaCredito > 0 Then

                        If codCompra > 0 Then

                            Dim dtCompraDetalle As New DataTable
                            dtCompraDetalle = controllerCompras.get_Remision_NotaCredito_ByID(oiml.dbConn, codDevolucion, codCompra)

                            If Not dtCompraDetalle Is Nothing Then
                                If dtCompraDetalle.Rows.Count > 0 Then
                                    For i As Integer = 0 To dtCompraDetalle.Rows.Count - 1

                                        If controllerVtas.Insert_NotasCredito_Detalle_Devolucion(oiml.dbConn, codNotaCredito, dtCompraDetalle.Rows(0)("Cantidad"), _
                                                                                                 dtCompraDetalle.Rows(0)("Mercaderia"), dtCompraDetalle.Rows(0)("iva"), _
                                                                                                 dtCompraDetalle.Rows(0)("precioFinal"), codCompra) Then
                                            CargarDatos()

                                        Else

                                            If Not controllerVtas.MessageError Is Nothing Then
                                                MessageBox.Show("Error: " & controllerVtas.MessageError.Message, "Notas de credito", MessageBoxButtons.OK, MessageBoxIcon.Error)
                                                Exit Sub
                                            End If

                                        End If

                                    Next

                                    MessageBox.Show("Registro grabado con exito!", "Notas de credito", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                                End If

                            End If

                        End If

                    End If

                Else

                    MessageBox.Show("No se registran datos de la devolucion ingresada. Favor verifique la cuenta del proveedor.", "Informe", MessageBoxButtons.OK, MessageBoxIcon.Information)

                End If

            End If

        End If

    End Sub

End Class